#include "stdafx.h"
#include "MoneyTableData.h"

CMoneyTableData::CMoneyTableData()
{
	m_arrFieldNames[FIELD_NAME_USER_ID] = "user_id";
	m_arrFieldNames[FIELD_NAME_ZONE_ID] = "zone_id";
	m_arrFieldNames[FIELD_NAME_BALANCE] = "balance";
	m_arrFieldNames[FIELD_NAME_SAVE_AMT] = "save_amt";
	m_arrFieldNames[FIELD_NAME_CHARGE_TIMES] = "charge_times";
}

CMoneyTableData::~CMoneyTableData()
{
	FreeTableData();
}

void CMoneyTableData::AddTableData(MYSQL_RES* pstMysqlRes)
{
	MYSQL_ROW curRow = mysql_fetch_row(pstMysqlRes);
	while (curRow != NULL)
	{
		CMoneyRowData* stData = new CMoneyRowData;
		if (stData == NULL)
		{
			break;
		}
		MYSQL_FIELD * field = pstMysqlRes->fields;
		if (field != NULL)
		{
			//stData->ParseRowData(curRow, field, pstMysqlRes->field_count);
			for (int i = 0; i < pstMysqlRes->field_count; i++)
			{
				std::string curFieldName = field->name;
				for (int j = 0; j < FIELD_NAME_MAX; j++)
				{
					if (this->m_arrFieldNames[j] == curFieldName)
					{
						stData->SetValue(j, curRow[i]);
						break;
					}
				}
				field++;
			}
			printf("\n");

			m_VecTableData.push_back(stData);
		}
		else
		{
			delete stData;
		}		
		curRow = mysql_fetch_row(pstMysqlRes);
	}
}

void CMoneyTableData::FreeTableData()
{
	std::vector<CMoneyRowData*>::iterator iter = m_VecTableData.begin();
	while (iter != m_VecTableData.end())
	{
		CMoneyRowData* stData = *iter;
		if (stData != NULL)
		{
			delete stData;
		}
		iter++;
	}
	m_VecTableData.clear();
}